package com.dycn.util;

import com.google.gson.*;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class CommGson {
    private static CommGson commGson;
    private static Gson gson;
    private static JsonParser jsonParser;

    private CommGson() {
    }

    public static CommGson getInstance() {
        if(commGson == null || gson == null) {
            Class var0 = CommGson.class;
            synchronized(CommGson.class) {
                if(commGson == null) {
                    commGson = new CommGson();
                    gson = new Gson();
                    jsonParser = new JsonParser();
                }
            }
        }

        return commGson;
    }

    public String toString(Object object) {
        return gson.toJson(object);
    }

    public JsonObject toJsonObject(String json) {
        return jsonParser.parse(json).getAsJsonObject();
    }

    public JsonArray toJsonArray(String json) {
        return jsonParser.parse(json).getAsJsonArray();
    }

    public <T> T toObject(String json, Class<T> t) {
        return gson.fromJson(json, t);
    }

    public static <T> List<T> toList(String json, Class<T> cls) {
        List reList = new ArrayList();
        JsonElement jsonElement = (new JsonParser()).parse(json);
        JsonArray array = jsonElement.getAsJsonArray();
        Iterator iterator = array.iterator();
        Gson gson = new Gson();

        while(iterator.hasNext()) {
            JsonElement json2 = (JsonElement)iterator.next();
            T contact = gson.fromJson(json2, cls);
            reList.add(contact);
        }

        return reList;
    }
}
